/**
 * 
 */
package com.feib.stms.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.feib.stms.dao.RoleDao;
import com.feib.stms.model.LabelValue;
import com.feib.stms.model.Role;
import com.feib.stms.model.User;
import com.feib.stms.service.RoleService;

/**
 * @author Karl Chen
 *
 */
@Service("roleService")
public class RoleServiceImpl extends BaseServiceImpl implements RoleService 
{
	private static final long serialVersionUID = -6319840719761064595L;
	
	@Resource(name="roleDao")
    private RoleDao roleDao;
	
	/**
	 * 根據pk查找Role
	 */
	public Role getByRolePk(String rolePk) {
		return roleDao.getByRolePk(rolePk);
	}

	/**
	 * 根據PK找到有權限的菜單 PK
	 */
	public List<String> getMenuItemPkByRoleId(String roleId) {
		return roleDao.getMenuItemPkByRoleId(roleId);
	}

	/**
	 * 查詢所有的系統功能角色
	 */
	public List<Role> getAllSystemRole() {
		return roleDao.getAllSystemRole();
	}	
	
	/**
	 * 列出該功能角色下的所有用戶
	 * @param roleNo
	 * @return
	 */
	public List<User> findAllSelectedRoleUser(String rolePk){
		return roleDao.getAllSelectedRoleUser(rolePk);
	}
	
	/**
	 * 列出當前GROUP下的所有用戶
	 * @param qryRoleNo
	 * @param userGroup
	 * @return
	 */
	public List<User> findAllSelectedRoleUserByGroupPk(String rolePk, Long userGroupPk){
		return roleDao.getAllSelectedRoleUserByGroupPk(rolePk, userGroupPk);
	}

	/**
	 * 列出當前GROUP下的用戶(除以選擇的用戶外)
	 * @param rolePk
	 * @param userGroupPk
	 * @return
	 */
	public List<User> findAllUnSelectedRoleUserByGroupPk(String rolePk, Long userGroupPk) {
		return roleDao.getAllUnSelectedRoleUserByGroupPk(rolePk, userGroupPk);
	}

	/**
	 * 新增角色菜單權限
	 * @param menuItemList
	 * @param rolePk
	 * @return
	 */
	public int insertRoleMenuItemPk(List<String> menuItemList, Long rolePk) {
		return roleDao.insertRoleMenuItemPk(menuItemList, rolePk);
	}

	/**
	 * 新增角色權限
	 * @param userList
	 * @param rolePk
	 * @return
	 */
	public int insertRoleUser(List<String> userList, Long rolePk) {
		return roleDao.insertRoleUser(userList, rolePk);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.feib.stms.service.RoleService#insert(com.feib.stms.model.Role, java.util.List, java.lang.Long, java.util.List)
	 */
	public boolean insert(Role role, List<String> tempActionBeans, Long rolePk, List<String> tempUser) {
		//新增功能角色
		int insertCount = roleDao.insert(role);
		//獲取新增角色主鍵
		rolePk = role.getRolePk();
		if(null == rolePk){
			return false;
		}
		//新增角色菜單權限
		int insertCount2 = roleDao.insertRoleMenuItemPk(tempActionBeans, rolePk);
		//新增角色權限
		int insertCount3 = roleDao.insertRoleUser(tempUser, rolePk);
		//添加對空數據的處理
		if(null == tempActionBeans){
			insertCount2 = 1;
		}else if(tempActionBeans.size() == 0){
			insertCount2 = 1;
		}
		if(null == tempUser){
			insertCount3 = 1;
		}else if(tempUser.size() == 0){
			insertCount3 = 1;
		}
		return insertCount > 0 && insertCount2 > 0 && insertCount3 > 0;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.feib.stms.service.RoleService#update(com.feib.stms.model.Role, java.util.List, java.lang.Long, java.util.List)
	 */
	public boolean update(Role role, List<String> tempActionBeans, Long rolePk, List<String> tempUser) {
		//更新功能角色
		int insertCount = roleDao.updateRole(role);
		//新增角色菜單權限  先刪后增
		roleDao.deleteRoleMenuItemByPk(rolePk);
		int insertCount2 = roleDao.insertRoleMenuItemPk(tempActionBeans, rolePk);
		//新增角色權限 先刪后增
		roleDao.deleteRoleUserByPk(rolePk);
		int insertCount3 = roleDao.insertRoleUser(tempUser, rolePk);
		//添加對空數據的處理
		if(null == tempActionBeans){
			insertCount2 = 1;
		}else if(tempActionBeans.size() == 0){
			insertCount2 = 1;
		}
		if(null == tempUser){
			insertCount3 = 1;
		}else if(tempUser.size() == 0){
			insertCount3 = 1;
		}
		//返回更新結果
		return insertCount > 0 && insertCount2 > 0 && insertCount3 > 0;
	}

	/**
     * 角色類別 Key-Value 格式資料
     * @return
     */
	public List<LabelValue> findLabelValueByGroupPk(String groupPk) {
        List<LabelValue> result = new ArrayList<LabelValue>();
        List<Role> roles = roleDao.findByGroupPk(groupPk);
        for (Role r : roles) {
            LabelValue lv = new LabelValue();
            lv.setLabel(r.getRoleName());
            lv.setValue(r.getRolePk().toString());
            result.add(lv);
        }

        return result;
	}
}
